//--------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>

#include <avr/interrupt.h>

#include "dkbasic.h"
#include "dktimer.h"
#include "dkevents.h"

//--------------------------------------------------------------------

volatile uint16_t dktimereventclock[NUM_TIMER_HANDLERS];
uint16_t dktimereventinterval[NUM_TIMER_HANDLERS];
uint16_t dktimereventtick;

uint8_t dktimer_flags=0;	// bitmapped for 8 timer events
uint8_t dktimer_masks=0;

//--------------------------------------------------------------------
ISR(TIMER1_OVF_vect) 
{
	int i;
	
	for( i=0; i<8; i++ )
	{
		if( ! (--dktimereventclock[i]) )
		{
			dktimer_flags |= (1<<i);
			if( dktimer_flags & dktimer_masks )
				dkeventflags |= DK_EVENT_TIMER;		// bits set to indicate which COM event fired	
			dktimereventclock[i] = dktimereventinterval[i];
		}
	}

	TCCR1B = 0;								// stop timer1
	TCNT1 = dktimereventtick;				// timer overflow every 1 second
	TCCR1B = ((1<<CS12) | (0<<CS11) | (1<<CS10));	// start timer at clk/1024			
}
//--------------------------------------------------------------------

